// pages/Index/Agent/add.js
const Controllers = require('../../controllers/Controller.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    agentId: '',
    shareAgentImg: '',
    show: false
  },
  
  
  shareAgent() {
    var that = this;
    var agentId = that.data.agentId;
    Controllers.shareAgent({agentId}).then(res => {
      console.log('shareAgent--res==', res);
      const { code, data, desc } = res;
      that.setData({
        shareAgentImg: data
      })
    })
  },
  shareAgentStaff() {
    var that = this;
    var agentId = that.data.agentId;
    Controllers.shareAgentStaff({agentStaffId: agentId}).then(res => {
      console.log('shareAgentStaff--res==', res);
      const { code, data, desc } = res;
      that.setData({
        shareAgentImg: data
      })
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log('options---', options.id);
    
    that.setData({
      agentId: parseInt(options.id),
      name: options.name || ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.judgeAgent(type => {
      console.log('judgeAgent--type==', type);
      if (type === 1) {
        that.shareAgent();
      } else if (type === 0) {
        that.shareAgentStaff();
      } else {
        wx.showToast({
          title: '代理商员工待接入',
          icon: 'none'
        })
      }
    })
  },
  changeModel() {
    var that = this;
    if (!that.data.shareAgentImg) {
      that.data.show = true;
    }
    that.setData({
      show: !that.data.show
    })
  },
  saveImg() {
    var that = this;
    var shareAgentImg = that.data.shareAgentImg;
    wx.getImageInfo({
      src: shareAgentImg,
      success: (res) => {
          let path = res.path;
          wx.saveImageToPhotosAlbum({
              filePath: path,
              success: (res) => {
                  console.log(res);
                  wx.showToast({
                      title: '已保存到相册',
                  })
              },
              fail: (res) => {
                  console.log('saveImageToPhotosAlbum--fail--res==', res);
              },
              complete: (res) => {
                that.changeModel();
              }
          })
      },
      fail: (res) => {
          console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      show: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      show: false
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})